<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="uid">
      <el-input v-model="dataForm.uid" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="文字内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="文字内容"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="imgs">
      <el-input v-model="dataForm.imgs" placeholder="图片"></el-input>
    </el-form-item>
    <el-form-item label="点赞量" prop="like">
      <el-input v-model="dataForm.like" placeholder="点赞量"></el-input>
    </el-form-item>
    <el-form-item label="地点" prop="location">
      <el-input v-model="dataForm.location" placeholder="地点"></el-input>
    </el-form-item>
    <el-form-item label="事件间隔" prop="timeinterval">
      <el-input v-model="dataForm.timeinterval" placeholder="事件间隔"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          uid: '',
          content: '',
          imgs: '',
          like: '',
          location: '',
          timeinterval: ''
        },
        dataRule: {
          uid: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '文字内容不能为空', trigger: 'blur' }
          ],
          imgs: [
            { required: true, message: '图片不能为空', trigger: 'blur' }
          ],
          like: [
            { required: true, message: '点赞量不能为空', trigger: 'blur' }
          ],
          location: [
            { required: true, message: '地点不能为空', trigger: 'blur' }
          ],
          timeinterval: [
            { required: true, message: '事件间隔不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/route/artical/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.uid = data.artical.uid
                this.dataForm.content = data.artical.content
                this.dataForm.imgs = data.artical.imgs
                this.dataForm.like = data.artical.like
                this.dataForm.location = data.artical.location
                this.dataForm.timeinterval = data.artical.timeinterval
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/route/artical/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'uid': this.dataForm.uid,
                'content': this.dataForm.content,
                'imgs': this.dataForm.imgs,
                'like': this.dataForm.like,
                'location': this.dataForm.location,
                'timeinterval': this.dataForm.timeinterval
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
